package com.shiku.imserver.common.http;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.http.common.Cookie;
import org.tio.http.common.HeaderName;
import org.tio.http.common.HeaderValue;
import org.tio.http.common.HttpResponseStatus;
import org.tio.utils.hutool.StrUtil;

public class HttpResponse extends HttpPacket {
    public static final HttpResponse NULL_RESPONSE = new HttpResponse();
    private static Logger log = LoggerFactory.getLogger(HttpResponse.class);
    private static final long serialVersionUID = -3512681144230291786L;
    private String charset;
    private List<Cookie> cookies;
    private boolean hasGzipped;
    private int headerByteCount;
    private Map<HeaderName, HeaderValue> headers;
    private boolean isStaticRes;
    private HttpRequest request;
    private boolean skipIpStat;
    private boolean skipTokenStat;
    private HttpResponseStatus status;

    public HttpResponse() {
        this.status = HttpResponseStatus.C200;
        this.isStaticRes = false;
        this.request = null;
        this.cookies = null;
        this.headers = new HashMap();
        this.headerByteCount = 2;
        this.hasGzipped = false;
        this.charset = "utf-8";
        this.skipIpStat = false;
        this.skipTokenStat = false;
    }

    public HttpResponse(HttpRequest request2) {
        this();
        this.request = request2;
        if (request2 != null && request2.httpConfig != null && request2.httpConfig.compatible1_0) {
            String connection = request2.getConnection();
            String str = request2.requestLine.version;
            char c = 65535;
            switch (str.hashCode()) {
                case 48563:
                    if (str.equals("1.0")) {
                        c = 0;
                        break;
                    }
                    break;
            }
            switch (c) {
                case 0:
                    if (StrUtil.equals(connection, "keep-alive")) {
                        addHeader(HeaderName.Connection, HeaderValue.Connection.keep_alive);
                        addHeader(HeaderName.Keep_Alive, HeaderValue.Keep_Alive.TIMEOUT_10_MAX_20);
                        return;
                    }
                    return;
                default:
                    if (StrUtil.equals(connection, "close")) {
                    }
                    return;
            }
        }
    }

    public HttpResponse(Map<HeaderName, HeaderValue> responseHeaders, byte[] body) {
        this.status = HttpResponseStatus.C200;
        this.isStaticRes = false;
        this.request = null;
        this.cookies = null;
        this.headers = new HashMap();
        this.headerByteCount = 2;
        this.hasGzipped = false;
        this.charset = "utf-8";
        this.skipIpStat = false;
        this.skipTokenStat = false;
        if (responseHeaders != null) {
            this.headers.putAll(responseHeaders);
        }
        setBody(body);
    }

    public void crossDomain() {
        addHeader(HeaderName.Access_Control_Allow_Origin, HeaderValue.from("*"));
        addHeader(HeaderName.Access_Control_Allow_Headers, HeaderValue.from("x-requested-with,content-type"));
    }

    public static HttpResponse cloneResponse(HttpRequest request2, HttpResponse response) {
        HttpResponse cloneResponse = new HttpResponse(request2);
        cloneResponse.setStatus(response.getStatus());
        cloneResponse.setBody(response.getBody());
        cloneResponse.setHasGzipped(response.isHasGzipped());
        cloneResponse.addHeaders(response.getHeaders());
        if (cloneResponse.getCookies() != null) {
            cloneResponse.getCookies().clear();
        }
        return cloneResponse;
    }

    public Map<HeaderName, HeaderValue> getHeaders() {
        return this.headers;
    }

    public void addHeader(HeaderName key, HeaderValue value) {
        this.headers.put(key, value);
        this.headerByteCount += key.bytes.length + value.bytes.length + 3;
    }

    public void addHeaders(Map<HeaderName, HeaderValue> headers2) {
        if (headers2 != null) {
            for (Map.Entry<HeaderName, HeaderValue> entry : headers2.entrySet()) {
                addHeader(entry.getKey(), entry.getValue());
            }
        }
    }

    public HeaderValue getContentType() {
        return this.headers.get(HeaderName.Content_Type);
    }

    public boolean addCookie(Cookie cookie) {
        if (this.cookies == null) {
            this.cookies = new ArrayList();
        }
        return this.cookies.add(cookie);
    }

    public String getCharset() {
        return this.charset;
    }

    public List<Cookie> getCookies() {
        return this.cookies;
    }

    public HttpRequest getHttpRequest() {
        return this.request;
    }

    public HttpResponseStatus getStatus() {
        return this.status;
    }

    public boolean isStaticRes() {
        return this.isStaticRes;
    }

    public String logstr() {
        if (this.request == null) {
            return "\r\n响应\r\n" + this.status.getHeaderText();
        }
        return ("\r\n响应: 请求ID_" + this.request.getId() + "  " + this.request.getRequestLine().getPathAndQuery()) + "\r\n" + getHeaderString();
    }

    public void setCharset(String charset2) {
        this.charset = charset2;
    }

    public void setCookies(List<Cookie> cookies2) {
        this.cookies = cookies2;
    }

    public void setHttpRequestPacket(HttpRequest request2) {
        this.request = request2;
    }

    public void setStaticRes(boolean isStaticRes2) {
        this.isStaticRes = isStaticRes2;
    }

    public void setStatus(HttpResponseStatus status2) {
        this.status = status2;
    }

    public boolean isHasGzipped() {
        return this.hasGzipped;
    }

    public void setHasGzipped(boolean hasGzipped2) {
        this.hasGzipped = hasGzipped2;
    }

    public boolean isSkipIpStat() {
        return this.skipIpStat;
    }

    public void setSkipIpStat(boolean skipIpStat2) {
        this.skipIpStat = skipIpStat2;
    }

    public boolean isSkipTokenStat() {
        return this.skipTokenStat;
    }

    public void setSkipTokenStat(boolean skipTokenStat2) {
        this.skipTokenStat = skipTokenStat2;
    }

    public HeaderValue getLastModified() {
        return getHeader(HeaderName.Last_Modified);
    }

    public HeaderValue getHeader(HeaderName name) {
        return this.headers.get(name);
    }

    public void setLastModified(HeaderValue lastModified) {
        if (lastModified != null) {
            addHeader(HeaderName.Last_Modified, lastModified);
        }
    }

    public String toString() {
        return this.status.toString();
    }

    public int getHeaderByteCount() {
        return this.headerByteCount;
    }
}
